import Vue from 'vue'
import Router from 'vue-router'
import Index from './views/index.vue'
import homeContent from './components/homeContent.vue'
import content from './components/content.vue'
import seachVue from './components/seachVue.vue'
import Err from './views/404.vue'
import login from './views/login.vue'
import HOME from './views/admin/home.vue'
import indexadmin from './views/admin/index.vue'
import userlist from './views/admin/userList.vue'
import type from './views/admin/type.vue'
import infoshow from './views/admin/infoshow.vue'
import UEditor from './views/admin/UEditor.vue'

Vue.use(Router)

const router =  new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/',
      redirect: '/index',
    },
    {
      path: '/index',
      name: 'index',
      component: Index,
      children:[
        {path:'',component:homeContent},
        {path:'/homeContent',name:'homeContent',component:homeContent},
        {path: '/content',name: 'content',component: content},
        {path: '/seachVue',name: 'seachVue',component: seachVue},
      ]
    },
    {
      path: '/admin/sanghongxv',
      name: 'login',
      component: login
    },
    {
      path: '/home',
      name: 'HOME',
      component: HOME,
      children: [
        {path:'',component: indexadmin},
        {path:'/indexadmin',name:'indexadmin', component: indexadmin},
        {path:'/userlist',name:'userlist', component: userlist},
        {path:'/type',name:'type', component: type},
        {path:'/infoshow',name:'infoshow', component: infoshow},
        {path:'/UEditor',name:'UEditor', component: UEditor},
      ]
    },
    {
      path: '*',
      name: '/404',
      component: Err
    }
  ]
})

router.beforeEach((to, from, next) => {
  const isLogin = localStorage.token ? true : false;
  if(to.path == "/admin/sanghongxv" || to.path == "/index" || to.path == "/content" ) {
    next();
  }else{
    isLogin ? next() : next('/index');
  }
})

export default router;